#ifndef VSMEDIA_H
#define VSMEDIA_H

#include "vsGlobal.h"
#include <QImage>


namespace vsTypes
{
    typedef enum
    {
        stopped = 0,
        checking,
        converting,
        downloading,
        seeding
    } fileStatus;
}

const QString& statusString(vsTypes::fileStatus status);


class vsMediaFile : public QObject
{
    Q_OBJECT

public:
    explicit vsMediaFile(const QString &path, QObject *parent = 0);
    ~vsMediaFile();
    inline bool operator==(const vsMediaFile &other) { return path == other.path; }

    QString id;
    QString path;
    QString name;
    QString ext;
    QString mime;
    qint64  size;

    int duration     = 0;
    int bitrate      = 0; // overall container bitrate
    int audioBitrate = 0;
    int videoBitrate = 0;

    QStringMap tags;

    QList<QImage> covers;

    QByteArray cachedCover;
    QSize      cachedCoverSize;
    QString    cachedCoverMime;

    vsTypes::fileStatus status;

    void onInfoUpdated();

public slots:
    //

signals:
    void statusChanged();
    void mediaInfoUpdated();

protected:
    QByteArray buffer;
};



/// custom info and settings for various audio and video formats
struct vsMediaTypeSettings
{
    QString ext; // file extension (or "suffix" in Qt)
    QString mime;

    vsMediaTypeSettings(const QString &ext = "", const QString &mime = "")
        : ext(ext), mime(mime)
    {}
};


class vsMedia : QObject
{
    Q_OBJECT

public:
    explicit vsMedia(QObject *parent = 0);
    static vsMedia *instance();
    ~vsMedia();

    bool supports(const QString &suffix, vsMediaTypeSettings *info);
    QString mime(const QString &suffix);

    QMap<QString, QString> supportedTypes(); /// ext + mime

    /** Asynchronous, uses vsMediaFile's mediaInfoUpdated() signal
        returns false on any error or failed check */
    bool requestMediaInfo(vsMediaFile* f);

public slots:
    //

signals:
    void getMediaInfo(vsMediaFile *f); // from controller (Phonon/ffmpeg)

protected:
    QMap<QString, vsMediaTypeSettings> formats;
    QStringList requestQueue;

private:
    Q_DISABLE_COPY(vsMedia)
};

#endif // VSMEDIA_H
